package com.sc.auction.auction.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.auction.auction.feign.UserFeignService;
import com.sc.auction.auction.pojo.Auction;
import com.sc.auction.auction.pojo.Handsel;
import com.sc.auction.auction.service.AuctionService;
import com.sc.auction.auction.service.HandselService;
import com.sc.auction.common.exception.AuctionException;
import com.sc.auction.common.pojo.User;
import com.sc.auction.common.util.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pgc
 * @since 2020-09-15
 */
@RestController
@RequestMapping("/auction/handsel")
@CrossOrigin
public class HandselController {

    @Autowired
    private HandselService handselService;

    @Autowired
    private AuctionService auctionService;

    @Autowired
    private UserFeignService userFeignService;

    @GetMapping("findAllHandsel")
    @ApiOperation("查询用户的保证金信息")
    public R findAllHandsel(@ApiParam(name = "page",defaultValue = "1",required = true)@RequestParam("page") Integer page,
                            @ApiParam(name = "limit",defaultValue = "5",required = true)@RequestParam("limit") Integer limit,
                            Integer userid){
        Page<Handsel> pageBean = new Page<>(page,limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userid",userid);
        handselService.page(pageBean,queryWrapper);
        List<Handsel> list = pageBean.getRecords();
        List<Handsel> list1 = new ArrayList<>();
        for (Handsel handsel: list
             ) {
            Auction auction = auctionService.getById(handsel.getAuctionId());
            handsel.setGoodsName(auction.getGoodsName());
            list1.add(handsel);
        }
        long total = pageBean.getTotal();
        if(list1!=null && !list1.isEmpty()){
            return R.ok().data("handselList",list1).data("total",total).code(200).message("查询用户保证金信息成功!");
        }else{
            return R.error().code(201).message("失败");
        }
    }


    @PutMapping(value = "/backHandselMoney")
    @ApiOperation("退还保证金")
    public R backHandselMoney(Integer id,Double money,Integer handselId) {

            R r = userFeignService.getUserById(id);
            Object go=r.getData().get("user");
            User user = JSON.toJavaObject((JSONObject)go,User.class);
            user.setBalance(user.getBalance().add(new BigDecimal(Double.valueOf(money))));
            R update = userFeignService.update(user);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id",handselId);
            Handsel handsel = new Handsel();
            handsel.setStatus(2);
            boolean update1 = handselService.update(handsel, queryWrapper);
        if(update.getCode()==200 && update1){
            return R.ok().code(200).message("退还押金成功!");
        }else{
            return R.error().code(201).message("爹不给了");
        }
    }


}

